package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.Policy;
import cn.wolfcode.crm.page.PageResult;
import cn.wolfcode.crm.query.PolicyQueryObject;
import cn.wolfcode.crm.service.ICarService;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.service.IPolicyService;
import cn.wolfcode.crm.service.IProductService;
import cn.wolfcode.crm.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/policy")
public class PolicyController {

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private ICarService carService;

    @Autowired
    private IPolicyService policyService;

    @Autowired
    private IProductService productService;

    @RequestMapping("add")
    public String addPolicy() {

        return "policy/addPolicy";
    }

    @RequestMapping("temporary")
    public String temporary() {

        return "policy/temporary";
    }
    @RequestMapping("auditing")
    public String auditing() {

        return "policy/auditing";
    }
    @RequestMapping("changing")
    public String changing() {

        return "policy/changing";
    }
    @RequestMapping("all")
    public String all() {

        return "policy/all";
    }

    @RequestMapping("temporaryList")
    @ResponseBody
    public PageResult temporaryList(PolicyQueryObject qo,Long state) {
        if(state!=null){

            qo.setState(state);
        }
        PageResult result = policyService.query(qo);
        return result;
    }
    //填写赔损信息列表
    @RequestMapping("listForLoss")
    @ResponseBody
    public PageResult listForLoss() {
        PageResult result = policyService.queryForLoss();
        return result;
    }
    //提审  改变状态即可
    @RequestMapping("putUp")
    @ResponseBody
    public JsonResult putUp(Long id) {

        JsonResult result = new JsonResult();
        try{
            policyService.changeState(id);
            result.setMessage("提审成功");
        }catch (Exception e){
            e.printStackTrace();
            result.mark("提审失败");
        }
        return  result;

    }
    //退回申请
    @RequestMapping("roll")
    @ResponseBody
    public JsonResult roll(Long id,String remark) {

        JsonResult result = new JsonResult();
        try{
            policyService.roll(id,remark);
            result.setMessage("退回申请成功");
        }catch (Exception e){
            e.printStackTrace();
            result.mark("退回申请失败");
        }
        return  result;

    }
    //提审  改变状态即可
    @RequestMapping("audit")
    @ResponseBody
    public JsonResult audit(Long id) {

        JsonResult result = new JsonResult();
        try{
            policyService.audit(id);
            result.setMessage("审核成功");
        }catch (Exception e){
            e.printStackTrace();
            result.mark("审核失败");
        }
        return  result;

    }
    //据审核  改变状态即可
    @RequestMapping("refuse")
    @ResponseBody
    public JsonResult refuse(Long id,String remark) {

        JsonResult result = new JsonResult();
        try{
            policyService.refuse(id,remark);
            result.setMessage("拒绝该用户投保申请成功");
        }catch (Exception e){
            e.printStackTrace();
            result.mark("拒绝该用户投保申请失败");
        }
        return  result;

    }

    @RequestMapping("policyDetail")
    @ResponseBody
    //编辑保单信息
    public Policy policyDetail(Long id) {

        Policy policy = new Policy();
        if (id != null) {
            //根据指定id获取保单对象
            policy = policyService.selectByPrimaryKey(id);
            Long custId = policy.getCustomer().getId();
            //获取客服的客户设置给当前的保单对象
            policy.setCustomer(customerService.selectByPrimaryKey(custId));
            //获取车辆信息设置给当前的保单对象
            policy.setCar(carService.selectByPrimaryKey(policy.getCar().getId()));

            policy.setProducts(productService.selectByPolicyId(policy.getId()));
        }
        return policy;
    }
    @RequestMapping("getAllSnInfo")
    @ResponseBody
    //编辑保单信息
    public List<Policy> getAllSnInfo() {
        return policyService.getAllSnInfo();
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Policy policy, Long pIds[]) {
        JsonResult result = new JsonResult();
        try {
            if (policy.getCustomer().getId() == null) {

                customerService.insert(policy.getCustomer());
            } else {

                customerService.updateByPrimaryKey(policy.getCustomer());
            }
            policy.getCar().setCustomer(policy.getCustomer());
            if (policy.getCar().getId() == null) {

                carService.insert(policy.getCar());
            } else {
                carService.updateByPrimaryKey(policy.getCar());

            }
            if (policy.getId() != null) {
                policyService.updateByPrimaryKey(policy, pIds);
            } else {
                policyService.insert(policy, pIds);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("操作失败");
        }

        return result;
    }


}
